<?php

/**
 * Author: Artem Azarov <bel-azar@ya.ru>
 * Date: 3/24/12
 * Time: 8:16 PM
 */
class UserIdentity extends CUserIdentity
{
    const ERROR_EMAIL_INVALID = 3;

    private $_id;

    public function authenticate()
    {
        if (strpos($this->username,"@")) {
            $user = User::model()->findByAttributes(array('email' => $this->username));
        } else {
            $user = User::model()->findByAttributes(array('login' => $this->username));
        }

        if($user===null) {
            if (strpos($this->username,"@")) {
                $this->errorCode = self::ERROR_EMAIL_INVALID;
            } else {
                $this->errorCode = self::ERROR_USERNAME_INVALID;
            }
        } else if(User::encrypting($this->password) !== $user->password) {
            $this->errorCode=self::ERROR_PASSWORD_INVALID;
        } else {
            $this->_id=$user->id;
            $this->username=$user->login;
            $this->errorCode=self::ERROR_NONE;
        }
        return $this->errorCode==self::ERROR_NONE;
    }

    public function getId()
    {
        return $this->_id;
    }


}
